package cn.cloudreal.cbms.common.util;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import cn.cloudreal.cbms.common.filter.UserSignFilter;

/**
 *时间转换工具类
 * 
 * @author
 * @version 2012--01-29
 * @see TimeTools
 * @since
 */
public class TimeTools
{
    private static Logger logger = Logger.getLogger(UserSignFilter.class);

    /**
     * Description:字符串"yyyy-MM-dd HH:mm:ss"<br>
     * 
     * @param dateString
     * @return Date (null)
     */
    public static Date getDate(String dateString)
    {
        if (dateString == null || "".equals(dateString))
        {
            return null;
        }

        Date date = null;
        try
        {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = df.parse(dateString);
        }
        catch (Exception e)
        {
            date = null;
        }
        return date;
    }

    /**
     * Description: 字符串"yyyy-MM-dd"<br>
     * 
     * @param dateString
     * @return Date (null)
     */
    public static Date getShortDate(String dateString)
    {
        if (dateString == null || "".equals(dateString))
        {
            return null;
        }

        Date date = null;
        try
        {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            date = df.parse(dateString);
        }
        catch (Exception e)
        {
            date = null;
        }
        return date;
    }

    /**
     * Description: ��s(yyyy-MM-dd HH:mm:ss)<br>
     * 
     * @param dateString
     * @return java.sql.Date
     */
    public static java.sql.Date getSqlDate(String dateString)
    {
        if (dateString == null || "".equals(dateString))
        {
            return null;
        }
        Date date = null;
        try
        {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = df.parse(dateString);
        }
        catch (Exception e)
        {
            return null;
        }
        return new java.sql.Date(date.getTime());
    }

    /**
     * Description:date <br>
     * 
     * @param dateString
     * @param format
     *            "yyyy-MM-dd HH:mm:ss"
     * @return Date (null)
     */
    public static Date getDateByFormat(String dateString, String format)
    {
        Date date = new Date();

        try
        {
            SimpleDateFormat df = new SimpleDateFormat(format);
            date = df.parse(dateString);
        }
        catch (Exception e)
        {
            // date = null;
            logger.error(e.toString());
        }

        return date;
    }

    /**
     * Description: sql<br>
     * 
     * @param dateString
     * @param format
     *            字符串"yyyy-MM-dd HH:mm:ss"
     * @return java.sql.Date
     */
    public static java.sql.Date getSqlDateByFormat(String dateString,
            String format)
    {
        if (dateString == null || "".equals(dateString))
        {
            return null;
        }
        Date date = null;
        try
        {
            SimpleDateFormat df = new SimpleDateFormat(format);
            date = df.parse(dateString);
        }
        catch (Exception e)
        {
            return null;
        }
        return new java.sql.Date(date.getTime());
    }

    public static String getString(Date date)
    {
        String dateString = "";
        try
        {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            dateString = df.format(date);
        }
        catch (Exception e)
        {
            dateString = "";
        }
        return dateString;
    }

    public static String getStringByFormat(Date date, String format)
    {
        String dateString = "";
        try
        {
            SimpleDateFormat df = new SimpleDateFormat(format);
            dateString = df.format(date);
        }
        catch (Exception e)
        {
            dateString = "";
        }
        return dateString;
    }

    public static String getFormatDateString(String dateString,
            String srcFormat,
            String destFormat)
    {
        Date date = null;
        String formatDateStr = "";
        try
        {
            SimpleDateFormat srcdf = new SimpleDateFormat(srcFormat);
            date = srcdf.parse(dateString);
            SimpleDateFormat destdf = new SimpleDateFormat(destFormat);
            formatDateStr = destdf.format(date);
        }
        catch (Exception e)
        {
            formatDateStr = dateString;
        }
        return formatDateStr;

    }

    /**
     * Description: 验证日期格式"yyyy/MM/dd hh:mm:ss"<br>
     * 
     * @param dateString
     *            2007/03/28 13:45:20
     * @return true 正确 false 错误
     * @exception/throws
     */
    public static boolean checkDateTimeSlash(String dateString)
    {
        // 日期格式正则表达式"yyyy/MM/dd hh:mm:ss"
        String re = "^\\d{4}\\/[0-1][0-9]\\/[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]$";

        return dateString.matches(re);
    }
}
